﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity;
using PBOffice.DAL;

namespace PBOffice.BLL
{
   public  class UserManager
    {

        #region 查询所有员工信息
        /// <summary>
        /// 查询所有员工信息
        /// </summary>
        /// <returns></returns>
        public List<v_UserDepPosition> ShowAllUserInfo()
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var users = from user in myOffice.v_UserDepPosition
                            select user;             //查询所有员工信息
                return users.ToList();               //返回员工信息集合
            }
        } 
        #endregion

        #region 查询员工信息（根据部门）
        /// <summary>
        /// 查询员工信息（根据部门）
        /// </summary>
        /// <param name="depNo">部门编号</param>
        /// <returns></returns>
        public List<v_UserDepPosition> ShowUserInfoByDep(int depNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var user = from u in myOffice.v_UserDepPosition
                           where u.DepNo==depNo
                           select u;              //根据部门查询员工信息
                return user.ToList();             //部门员工信息
            }
        }

       /// <summary>
       /// 根据部门、职位、姓名联合查询员工信息
       /// </summary>
       /// <param name="posNo">职位</param>
       /// <returns></returns>
        public List<v_UserDepPosition> ShowUserInfoByUserInfo(v_UserDepPosition userInfo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var user = from u in myOffice.v_UserDepPosition
                           where u.PosNo==userInfo.PosNo||u.DepId==userInfo.DepId||u.UserName.Contains(userInfo.UserName)
                           select u;             //根据部门、职位、姓名联合查询员工信息
                return user.ToList();             //员工信息
            }
        }

       /// <summary>
       /// 根据姓名查询员工信息(模糊查询)
       /// </summary>
       /// <param name="userName">姓名</param>
       /// <returns></returns>
        public List<v_UserDepPosition> ShowUserInfoByName(string userName)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var user = from u in myOffice.v_UserDepPosition
                           where u.UserName.Contains(userName)
                           select u;              //根据员工姓名查询员工信息
                return user.ToList();             //部门员工信息
            }
        }
        /// <summary>
        /// 根据编号查询员工信息
        /// </summary>
        /// <param name="userName">编号</param>
        /// <returns></returns>
        public v_UserDepPosition ShowUserInfoByUserNo(int userNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var user = from u in myOffice.v_UserDepPosition
                           where u.UserNo==userNo
                           select u;              //根据员工姓名查询员工信息
                return (v_UserDepPosition)user;             //部门员工信息
            }
        }
        /// <summary>
        /// 根据直接上级编号查询员工信息
        /// </summary>
        /// <param name="userName">编号</param>
        /// <returns></returns>
        public List<v_UserDepPosition> ShowUserInfoByUserLeaderNo(int userLeaderNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var user = from u in myOffice.v_UserDepPosition
                           where u.UserLeaderNo == userLeaderNo
                           select u;              //根据上级编号查询员工信息
                return user.ToList();           
            }
        }

        #endregion

        #region 添加员工信息
        /// <summary>
        /// 添加员工信息
        /// </summary>
        /// <param name="u">员工对象</param>
        /// <returns></returns>
        public bool AddUserInfo(User u,int depNo,int posNo,int dipNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                User user = new User
                {
                    UserId = u.UserId, //用户名
                    UserPwd = u.UserPwd, //密码
                    UserName = u.UserName, //姓名
                    UserGender = u.UserGender, //性别
                    UserLeaderNo = u.UserLeaderNo, //直接上级
                    UserCareer = u.UserCareer,     //工作经历
                    UserEntryTime = u.UserEntryTime, //入职时间
                    UserBirthday = u.UserBirthday, //出生日期
                    UserLandline = u.UserLandline, //座机
                    UserMobile = u.UserMobile, //手机
                    UserAddr = u.UserAddr, //住址
                    UserPostcode = u.UserPostcode, //邮编
                    Email = u.Email,//Email
                    UserStatus = u.UserStatus //用户状态
                };
                user.Department=myOffice.Departments.SingleOrDefault(dep =>dep.DepNo==depNo);//部门编号
                user.Position = myOffice.Positions.SingleOrDefault(pos =>pos.PosNo==posNo);    //职位编号
                user.UserDiploma=myOffice.UserDiplomas.SingleOrDefault(dip => dip.UserDiplomaNo==dipNo); //学历编号
                myOffice.Users.AddObject(user);            //添加用户
                return myOffice.SaveChanges() > 0;           //添加成功，返回true；否则，返回false
            }
        } 
        #endregion

        #region 修改员工信息（根据员工编号）
        /// <summary>
        /// 修改员工信息（根据员工编号）
        /// </summary>
        /// <param name="u">员工对象</param>
        /// <returns></returns>
        public bool ChangeUserInfo(User u,int depNo,int posNo,int dipNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var user1 = from user2 in myOffice.Users
                            where user2.UserNo == u.UserNo
                            select user2;                //根据编号查询用户信息
                user1.First();                //获取结果集中的第一个元素
                User user = (User)user1;     //类型转换
                user.UserId = u.UserId;      //用户名
                user.UserPwd = u.UserPwd;    //密码
                user.UserName = u.UserName;  //姓名
                user.UserGender = u.UserGender;  //性别
                user.Department = myOffice.Departments.SingleOrDefault(dep => dep.DepNo==depNo);  //部门编号
                user.Position = myOffice.Positions.SingleOrDefault(pos=>pos.PosNo==posNo);      //职位编号
                user.UserCareer = u.UserCareer;              //工作经历
                user.UserLeaderNo = u.UserLeaderNo;          //直接上级
                user.UserDiploma = myOffice.UserDiplomas.SingleOrDefault(dip=>dip.UserDiplomaNo==dipNo);   //学历编号
                user.UserEntryTime = u.UserEntryTime;         //入职日期
                user.UserBirthday = u.UserBirthday;           //生日
                user.UserLandline = u.UserLandline;           //座机
                user.UserMobile = u.UserMobile;               //手机
                user.UserAddr = u.UserAddr;                   //住址
                user.UserPostcode = u.UserPostcode;           //邮编
                user.UserHeadimg = u.UserHeadimg;             //头像
                user.Email = u.Email;                         //Email
                user.UserStatus = u.UserStatus;               //状态
                return myOffice.SaveChanges() > 0;              //保存修改后的用户信息，修改成功，返回true；否则，返回false
            }
        }
        #endregion

        #region 删除员工信息（根据员工编号）
        /// <summary>
        /// 删除员工信息（根据员工编号）
        /// </summary>
        /// <param name="userNo"></param>
        /// <returns></returns>
        public bool DeleteUserInfo(int userNo)
        {
            using (MyOfficeEntities myOffice = new MyOfficeEntities())
            {
                var user1 = from u in myOffice.Users
                            where u.UserNo == userNo
                            select u;               //根据员工编号查询员工信息
                user1.First();                     //从结果集中获取第一个元素
                User user = (User)user1;           //类型转换
                myOffice.Users.DeleteObject(user);    //从数据库中删除用户信息
                return myOffice.SaveChanges() > 0;     //保存数据库中删除后的数据，删除成功，返回true；否则，返回false
            }
        } 
        #endregion
       
    }
}
